/**
 * Created by Administrator on 2018/5/19.
 * 素材分享方法
 */
const app = getApp(),
  _ = require('../Common/underscore.js'),
  {
    $resHost
  } = app.globalData;
/**
 *
 * @param type 素材类型
 * @param dataset 素材数据
 * @param material 素材
 * @returns {{title: *, path: (*|string), successCallback: (function())}}
 */
module.exports = (type, dataset, material) => {
  const webViewUrl = '/pages/WebView/webView?url=user&webHash=';
  let {
      articletype,
      articlewechat,
      id,
      title,
      thumb = '',
      materialId,
      params = {}
    } = dataset,
    {
      userId,
      name
    } = xhs.getStorageSync('employeeInfo'),
    success = app.$shareSuccess(type, id, userId, ''),
    webViewQuery = encodeURIComponent(`shareEmployeeId=${userId}`),
    path = '',
    materialTypeName = '';
  switch (type) {
    case '12':
      // path = `${webViewUrl}coupon/manage/info/${id}&query=${webViewQuery}`;
      path = `/pages/User/Coupon/get?id=${id}&shareEmployeeId=${userId}`;
      thumb = thumb || `${$resHost}/img/share/coupon_share.jpg`;
      title = name + '赠送给您一张优惠券,赶快来领取吧~';
      materialTypeName = '优惠券';
      break;
    case '22':
      path = `${webViewUrl}store/service/${id}&query=${webViewQuery}`;
      materialTypeName = '服务商品';
      break;
    case '6':
      path = `/pages/User/Activity/Info/info?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '报名';
      break;
    case '19':
      if (articletype == '2') {
        path = `/pages/WebView/webView?url=https&https=${articlewechat}`;
      } else {
        path = `/pages/User/Article/info?id=${id}&shareEmployeeId=${userId}`;
      }
      materialTypeName = '文章';
      break;
    case '8':
      path = `/independentGoods/Goods/goods?goodsId=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '商品';
      break;
    case '121':
      path = `/independentNewGoods/Goods/index?goodsId=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '商品';
      break;
    case '26':
      path = `/pages/Employee/Material/caseDetail?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '案例';
      break;
    case '4':
      path = `/independentBargain/Bargain/Detail/detail?bargainId=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '砍价';
      break;
    case '43':
      path = `/pages/User/Choose/index?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '评选';
      break;
    case '3':
      xhs.showShareMenu({
        withShareTicket: true
      });
      path = `/independentCollage/Collage/Detail/detail?id=${id}&userId=${userId}&shareEmployeeId=${userId}&type=${params.type}&groupId=0`;
      success = res => {
        app.$shareSuccess(type, id, userId, '');
        if (res.shareTickets != undefined) {
          xhs.getShareInfo({
            shareTicket: res.shareTickets[0],
            complete(resEr) {
              //创建团ID
              app.$postData('/employee/groupon/create-group', app.$parseParam({
                grouponId: id,
                iv: resEr.iv,
                encryptedData: resEr.encryptedData
              }, miniProgrammMaterial)).then(data => {
                if (data.errCode == 0) {
                  xhs.showToast({
                    title: '创建成功',
                    icon: 'success',
                    duration: 1000
                  });
                } else {
                  xhs.showToast({
                    title: data.errInfo,
                    icon: 'success',
                    duration: 1000
                  });
                }
              });
            }
          });
        }
      };
      break;
    case '20':
      path = `/independentKill/Kill/Detail/detail?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '秒杀';
      break;
    case '106':
      path = `/pages/User/luckDraw/Detail/detail?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '免费抽奖';
      //免费抽奖默认图片处理
      if (type == '106' && !dataset.params.materialId && dataset.params.mainPic && !dataset.params.mainPic.length || !dataset.params.mainPic && dataset.params.materialId && !dataset.params.materialId.length) {
        materialId = [{
          thumb: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`
        }];
      }
      break;
    case '107':
      path = `/pages/User/CollectFavour/Details/details?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '集赞';
      title = params.shareTitle || params.title;
      materialId = _.isEmpty(params.shareImgId) ? [{
        url: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`,
        thumb: `${$resHost}/img/miniprogram/luckDraw/default-img.jpg`
      }] : params.shareImgId;
      break;
    case '108':
      path = `/independentCrowdFunding/CrowdFunding/Detail/detail?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '众筹';
      break;
    case '110':
      path = `/independentWishAssist/WishAssist/Detail/detail?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '心愿购';
      break;
    case '112':
      if (dataset.params.lotteryWay == '2') {
        path = `/pages/User/Turn/Tiger/detail?id=${id}&shareEmployeeId=${userId}`;
      } else if (dataset.params.lotteryWay == '3') {
        path = `/pages/User/Turn/shakeDetail/detail?id=${id}&shareEmployeeId=${userId}`;
      } else {
        path = `/pages/User/Turn/Detail/detail?id=${id}&shareEmployeeId=${userId}`;
      }
      materialTypeName = '转盘抽奖';
      //转盘抽奖默认图片处理
      if (type == '112' && !dataset.params.materialId && dataset.params.mainPic && !dataset.params.mainPic.length || !dataset.params.mainPic && dataset.params.materialId && !dataset.params.materialId.length) {
        materialId = [{
          thumb: `${$resHost}/img/miniprogram/turn/banner.jpg`
        }];
      }
      break;
    case '113':
      path = `/pages/User/Jifu/index?id=${id}&shareEmployeeId=${userId}`;
      materialTypeName = '集福';
      break;
    case '114':
      path = `/pages/User/Discover/detail?id=${id}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '推荐有礼';
      break;
    case '130':
      path = `/independentPresell/Detail/index?id=${id}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '商品';
      break;
    case '126':
      path = `/pages/Employee/videoList/videoDetails/details?id=${id}&shareEmployeeId=${userId}&shareUserId=${userId}`;
      materialTypeName = '短视频';
      break;
  }
  return {
    materialTypeName,
    title,
    imageUrl: thumb || materialId[0].thumb,
    path,
    success
  };
};